﻿
Imports NearAop

<AddProxyProperty>
Public Class T03_属性代理
    Implements IAopHandlerProperty
    Property MyProperty As Integer
    Public Sub PropertySetHandler(Of T)(aopParamInfo As Lazy(Of AopParamInfo), sender As Object, value As T, getValue As Func(Of T), setValue As Action(Of T)) Implements IAopHandlerPropertySet.PropertySetHandler
        Dim propName = aopParamInfo.GetMemberName()
        Dim msg = $"{sender}.{propName}被修改, 修改前:{getValue()}, 新值:{value}"
        Console.WriteLine(msg)
        setValue(value) '执行修改
    End Sub

    Public Function PropertyGetHandler(Of T)(aopParamInfo As Lazy(Of AopParamInfo), sender As Object, getValue As Func(Of T)) As T Implements IAopHandlerPropertyGet.PropertyGetHandler
        Dim propName = aopParamInfo.GetMemberName()
        Dim result = getValue()
        Dim msg = $"{sender}.{propName}被读取,值:{result}"
        Console.WriteLine(msg)
        Return result
    End Function

    Sub Test()
        MyProperty += 1
        MyProperty += 1
    End Sub
End Class


Class T03_属性代理_等效写法
    Implements IAopHandlerPropertyLocal
    Property MyProperty As Integer
    Public Sub PropertySetHandler(Of T)(aopParamInfo As Lazy(Of AopParamInfo), sender As Object, value As T, getValue As Func(Of T), setValue As Action(Of T)) Implements IAopHandlerPropertySet.PropertySetHandler
        Dim propName = aopParamInfo.GetMemberName()
        Dim msg = $"{sender}.{propName}被修改, 修改前:{getValue()}, 新值:{value}"
        Console.WriteLine(msg)
        setValue(value) '执行修改
    End Sub

    Public Function PropertyGetHandler(Of T)(aopParamInfo As Lazy(Of AopParamInfo), sender As Object, getValue As Func(Of T)) As T Implements IAopHandlerPropertyGet.PropertyGetHandler
        Dim propName = aopParamInfo.GetMemberName()
        Dim result = getValue()
        Dim msg = $"{sender}.{propName}被读取,值:{result}"
        Console.WriteLine(msg)
        Return result
    End Function

    Sub Test()
        MyProperty += 1
        MyProperty += 1
    End Sub
End Class